<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm">
    <el-form-item label="Password" prop="pass">
      <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="Confirm" prop="checkPass">
      <el-input
        v-model="ruleForm.checkPass"
        type="password"
        autocomplete="off"
      />
    </el-form-item>
    <el-form-item label="Age" prop="age">
      <el-input v-model.number="ruleForm.age" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(this.$refs.ruleFormRef)">
        Submit
      </el-button>
      <el-button @click="resetForm(this.$refs.ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { ref } from 'vue'

let _valid_vo;

export default{
    mounted(){
      console.log("组件创挂完成")
      _valid_vo = this.ruleForm
    },
    data(){
        return {
            ruleForm:{
                pass:"",
                checkPass:"",
                age:""
            },
            rules:{
               pass :[{
                   validator: function(r,v,c){
                        let reg= /^\w{6,18}$/;
                        if(!reg.test(v)){
                            c(new Error('密码只支持英文和数字，长度为6-18位！'))
                        }else{
                            c()
                        }
                    }, trigger: 'blur' 
                }],
                checkPass:[
                    {validator: function(r,v,c){
                        if(v !== _valid_vo.pass){
                            c(new Error('两次输入的密码不一致'))
                        }else{
                            c()
                        }
                    }, trigger: 'blur'}
                ]  
            }
        }
    },
    methods: {
      submitForm:function(form){
        if (!form) return
        form.validate((valid) => {
          if (valid) {
            console.log('submit!')
          } else {
            console.log('error submit!')
          }
        })
      },
      resetForm:function(form){
        if (!form) return
        form.resetFields()
      }
    }
}
</script>